import collections
class Solution(object):
    def maxOperations(self, nums, k):
        c = collections.Counter(nums)
        ans = 0
        for key in c:
            if k - key == key:
                ans += c[key] // 2
            else:
                min_val = min(c[key], c[k - key])
                c[key] -= min_val
                c[k - key] -= min_val
                ans += min_val
        return ans